﻿using System;
using System.Data;
using System.Data.OleDb;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.IO;
using Bass.Tools;
using MySql.Data.MySqlClient;

public partial class admin_item_list : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        string temp = Convert.ToString(Session["adminname"]);
        if (Convert.ToString(Session["adminname"]) == "")
        {
            Response.Write("<script language=javascript>alert('您没有登录');window.parent.document.location.href ='admin_login.aspx'</script>");
        }
        else if (Convert.ToString(Session["adminkind"]) != "super")
        {
            Response.Write("<script language=javascript>alert('您不是产品管理员，无权访问此页！');location='admin_login.aspx'</script>");
        }

        else if (!this.IsPostBack)
        {
            this.bind();
        }
    }

    public void bind()
    {
        //OleDbConnection Conn = new OleDbConnection();
        //Conn.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;" + "Data Source=" + Server.MapPath("../miumiu.mdb");
        //Conn.Open();

        //取到连接字符串
        string propertiesPath = Server.MapPath("../mysqldb.properties");
        mytools mt = new mytools();
        string ConnString = mt.getMysqlConn(propertiesPath);

        //打开连接
        MySqlConnection myConnection = new MySqlConnection(ConnString);
        myConnection.Open();

        MySqlDataAdapter sda = new MySqlDataAdapter("select * from product_info where productstatus='A' order by addtime desc", myConnection);
        DataSet ds = new DataSet();
        sda.Fill(ds, "item_list");
        this.GridView1.DataSource = ds.Tables["item_list"];
        this.GridView1.DataKeyNames = new string[] { "productnum" };
        this.GridView1.DataBind();
        myConnection.Close();
    }

    protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        this.GridView1.PageIndex = e.NewPageIndex;
        this.bind();
    }

    protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            ((LinkButton)(e.Row.Cells[8].Controls[0])).Attributes.Add("onclick", "return confirm('确定删除吗？')");
        }
    }

    protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {
        string productnum = this.GridView1.DataKeys[e.RowIndex].Value.ToString();

        //连接数据库
        //OleDbConnection Conn = new OleDbConnection();
        //Conn.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;" + "Data Source=" + Server.MapPath("../miumiu.mdb");
        //Conn.Open();

        //取到连接字符串
        string propertiesPath = Server.MapPath("../mysqldb.properties");
        mytools mt = new mytools();
        string ConnString = mt.getMysqlConn(propertiesPath);

        //打开连接
        MySqlConnection myConnection = new MySqlConnection(ConnString);
        myConnection.Open();
 
        //删除数据库记录
        string sql = "delete from product_info where productnum= '" + productnum + "'";
        MySqlCommand scd = new MySqlCommand(sql, myConnection);
        scd.ExecuteNonQuery();
        myConnection.Close();

        this.bind();

        //Response.Write("<script language=javascript>alert('操作成功');location='item_list.aspx'</script>");
        Response.Write("<script language=javascript>alert('操作成功');window.parent.main.location.href = 'item_list.aspx';</script>");

    }
}
